package refactor.service.impl;

import app.utils.Md5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import common.standard.table.UserFieldEnum;
import common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import refactor.entity.User;
import refactor.mapper.UserMapper;
import refactor.service.IUserService;

import java.util.List;

/**
 * <p>
 * 用户表：记录使用此系统的用户相关数据 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2019-08-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

//    @Autowired
//    public void setUserMapper(UserMapper userMapper) {
//        this.userMapper = userMapper;
//    }


    /**
     * 处理用户登录
     *
     * @param name     用户名
     * @param password 用户密码
     * @return 返回null表示不存在该用户，否则返回用户对象
     */
    @Override
    public User userLogin(String name, String password) {
        Md5 md5 = new Md5();

        String nameDigest = md5.getDigest(name);
        String passwordDigest = md5.getDigest(password);

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>()
                .eq(UserFieldEnum.NAME_DIGEST.getFieldName(), nameDigest)
                .eq(UserFieldEnum.PASSWORD_DIGEST.getFieldName(), passwordDigest);


        return userMapper.selectOne(userQueryWrapper);
    }

    /**
     * 展示某个的开门人
     *
     * @param deviceId 设备id
     * @return 用户list集合
     */
    @Override
    public List<UserVo> showAccessController(Integer deviceId) {
        return userMapper.showOperator(deviceId);
    }

    public static void main(String[] args) {
        Md5 md5 = new Md5();
        System.out.println(md5.getDigest("13308967234"));
    }
}
